package cn.sh.basic.service.impl;

import cn.sh.basic.mapper.BaseMapper;
import cn.sh.basic.page.PageList;
import cn.sh.basic.query.BaseQuery;
import cn.sh.basic.service.IBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public abstract class BaseServiceImpl<T> implements IBaseService<T> {

    @Autowired
    private BaseMapper<T> mapper;

    @Override
    public List selectAll() {
        return mapper.selectAll();
    }

    @Override
    public T selectOne(Long id) {
        return mapper.selectOne(id);
    }

    @Override
    @Transactional
    public void add(T t) {
        mapper.add(t);
    }

    @Override
    @Transactional
    public void update(T t) {
        mapper.update(t);
    }

    @Override
    @Transactional
    public void delete(Long id) {
        mapper.delete(id);
    }

    @Override
    public PageList<T> queryData(BaseQuery baseQuery) {
        //查询总条数
        Integer count = mapper.selectCount(baseQuery);
        //查询当前页的数据
        List<T> data = mapper.selectData(baseQuery);

        return new PageList<T>(count,data);
    }

    @Override
    public void batchDelete(List ids) {
        mapper.batchDelete(ids);
    }
}
